package ru.arriah.servicedesk.web.client.action;

import java.io.IOException;
import java.util.Collection;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ru.arriah.common.web.action.ActionMapping;
import ru.arriah.common.web.action.DumbAction;
import ru.arriah.servicedesk.bean.ClientBean;
import ru.arriah.servicedesk.bean.DepartmentBean;
import ru.arriah.servicedesk.bean.RequestBean;
import ru.arriah.servicedesk.ejb.ClientManagerLocalHome;
import ru.arriah.servicedesk.ejb.ClientManagerLocalObject;
import ru.arriah.servicedesk.ejb.OrganizationManagerLocalHome;
import ru.arriah.servicedesk.ejb.OrganizationManagerLocalObject;
import ru.arriah.servicedesk.ejb.RequestManagerLocalHome;
import ru.arriah.servicedesk.ejb.RequestManagerLocalObject;
import ru.arriah.servicedesk.help.Utils;
import ru.arriah.servicedesk.web.commonaction.exception.InternalException;

public class DisplayRequestFormAction extends DumbAction {
  protected RequestManagerLocalHome requestManagerHome;
  protected OrganizationManagerLocalHome organizationManagerHome;
  protected ClientManagerLocalHome clientManagerHome;
  
  public DisplayRequestFormAction() throws NamingException {
    super();
    
    requestManagerHome =
       Utils.getRequestManagerHomeInterface();
    
    organizationManagerHome =
      Utils.getOrganizationManagerHomeInterface();
    
    clientManagerHome = 
      Utils.getClientManagerHomeInterface();
  }
  
  public String execute(
    ActionMapping actionMapping,
    HttpServletRequest request,
    HttpServletResponse response
  ) throws IOException, ServletException {
    try {
      RequestManagerLocalObject requestManager = 
        requestManagerHome.create();      
      
      OrganizationManagerLocalObject organizationManager = 
        organizationManagerHome.create();
      
      ClientManagerLocalObject clientManager = 
        clientManagerHome.create();                   
      
      int clientId = Utils.getUserId(request);
      ClientBean clientBean = clientManager.selectClient(clientId);
      
      Collection<DepartmentBean> departmentsList =
        organizationManager.selectDepartments(clientBean.getOrganizationId());
                  
      requestManager.remove();
      organizationManager.remove();
      clientManager.remove();
      
      RequestBean requestBean = new RequestBean();
      requestBean.setDepartmentId(clientBean.getDepartmentId());
      requestBean.setOrganizationId(clientBean.getOrganizationId());           
                  
      request.setAttribute("departmentsList", departmentsList);
      request.setAttribute("requestBean", requestBean);
      
      return actionMapping.getSuccessTarget();
    } catch (Exception e) {
      handleException(e, "execute");
      throw new InternalException("ErrorMessage.internal");
    }
  }
}